﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Administration.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class CommitteesListViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdminRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommitteesListViewModel"/> class.
        /// </summary>
        /// <param name="AdministrationRepository">The administration repository.</param>
        public CommitteesListViewModel(IAdministrationRepository AdministrationRepository)
        {
            _AdminRepository = AdministrationRepository;
        }

        /// <summary>
        /// Gets all committees.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ICommittee> GetAllCommittees()
        {
            return this._AdminRepository.FindCommittees(C => true);
        }

        /// <summary>
        /// Gets the committee by ID.
        /// </summary>
        /// <param name="CommitteeID">The committee ID.</param>
        /// <returns></returns>
        public ICommittee GetCommitteeByID(Guid CommitteeID)
        {
            return this._AdminRepository.GetCommitteeByID(CommitteeID);
        }

        /// <summary>
        /// Gets all centers.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ICenter> GetAllCenters()
        {
            return this._AdminRepository.FindCenters(C => true);
        }

        /// <summary>
        /// Gets the center by ID.
        /// </summary>
        /// <param name="CenterID">The center ID.</param>
        /// <returns></returns>
        public ICenter GetCenterByID(Guid CenterID)
        {
            return this._AdminRepository.GetCenterByID(CenterID);
        }

        /// <summary>
        /// Gets the village by ID.
        /// </summary>
        /// <param name="VillageID">The village ID.</param>
        /// <returns></returns>
        public IVillage GetVillageByID(Guid VillageID)
        {
            return this._AdminRepository.GetVillageByID(VillageID);
        }

        /// <summary>
        /// Updates the committee.
        /// </summary>
        /// <param name="CommitteeID">The committee ID.</param>
        /// <param name="CenterID">The center ID.</param>
        /// <param name="CommitteeName">Name of the committee.</param>
        /// <param name="VillageList">The village list.</param>
        /// <param name="ProjectAccounts">The project accounts.</param>
        public void UpdateCommittee(Guid CommitteeID, Guid CenterID, string CommitteeName, IEnumerable<IVillage> VillageList, Dictionary<Guid, string> ProjectAccounts)
        {
            using (IUnitOfWork transaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                ICommittee editCommittee = _AdminRepository.GetCommitteeByID(CommitteeID);
                editCommittee.CenterID = CenterID;
                editCommittee.CommitteeName = CommitteeName;

                //var villages = _AdminRepository.FindVillages(t => VillageList.Select(v => v.VillageID).Contains(t.VillageID));
                //_AdminRepository.DeleteCommitteeVillages(editCommittee);
                editCommittee.ChangeVillages(VillageList);

                ProjectAccounts.ForEach(pro =>
                    {
                        editCommittee.CommitteeBankAccounts.Where(bankAcc => bankAcc.ProjectID.Equals(pro.Key)).ForEach(account =>
                        {
                            account.BankAccountNumber = pro.Value;
                        });

                    });


                transaction.Commit();

            }
        }

        /// <summary>
        /// Creates New commmittee.
        /// </summary>
        /// <param name="CenterID">The center ID.</param>
        /// <param name="CommitteeName">Name of the committee.</param>
        /// <param name="VillageList">The village list.</param>
        /// <param name="ProjectAccounts">The project accounts.</param>
        public void CreateCommmittee(Guid CenterID, string CommitteeName, IEnumerable<IVillage> VillageList, Dictionary<Guid, string> ProjectAccounts)
        {
            using (IUnitOfWork transaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                ICommittee Committee = ServiceLocator.Current.GetInstance<ICommittee>();
                Committee.CommitteeID = Guid.NewGuid();
                Committee.CenterID = CenterID;
                Committee.CommitteeName = CommitteeName;

                Committee.ChangeVillages(VillageList);
                //Committee.Villages = newVillageList;
                _AdminRepository.CreateCommittee(Committee);


                ProjectAccounts.ForEach(projectData =>
                    {

                        ICommitteeBankAccount commBankAccount = ServiceLocator.Current.GetInstance<ICommitteeBankAccount>();
                        commBankAccount.CommitteeBankAccountID = Guid.NewGuid();
                        commBankAccount.CommitteeID = Committee.CommitteeID;
                        commBankAccount.ProjectID = projectData.Key;
                        commBankAccount.BankAccountNumber = projectData.Value;
                        _AdminRepository.CreateCommitteeBankAccount(commBankAccount);

                    });


                //                var allProjects = _AdminRepository.FindProjects(project => true);

                //                allProjects.ForEach(project =>
                //                {
                //                    ICommitteeBankAccount commBankAccount = ServiceLocator.Current.GetInstance<ICommitteeBankAccount>();
                //                    commBankAccount.CommitteeBankAccountID = Guid.NewGuid();
                //                    commBankAccount.CommitteeID = Committee.CommitteeID;
                //                    commBankAccount.ProjectID = project.ProjectID;
                //#warning AccountNumber inserted by wrong value
                //                    commBankAccount.BankAccountNumber = DateTime.Now.Second.ToString();
                //                    _AdminRepository.CreateCommitteeBankAccount(commBankAccount);
                //                }
                //            );

                transaction.Commit();
            }
        }

        /// <summary>
        /// Doeses the committee exist.
        /// </summary>
        /// <param name="ComitteeName">Name of the comittee.</param>
        /// <returns></returns>
        public bool DoesCommitteeExist(string ComitteeName)
        {
            IEnumerable<ICommittee> committees = _AdminRepository.FindCommittees(c => c.CommitteeName.Contains(ComitteeName));
            if (committees.Count() > 0)
                return true;
            else
                return false;
        }


        /// <summary>
        /// Gets all projects.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IProject> GetAllProjects()
        {
            return _AdminRepository.FindProjects(pro => true);
        }

    }
}
